﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YasharEl.Infrastructure.Plugins;
using System.IO;
using System.Windows;
using System.Windows.Markup;
using System.ComponentModel.Composition.Hosting;
using YasharEl.Infrastructure.PresentationModel.MVVM;
using System.ComponentModel.Composition.Primitives;
using YasharEl.Infrastructure.Plugins.Administration;

namespace RemoteTranslator.AssistentApplication
{
    public partial class RemoteTranslatorAssistentApp : BasePluginsApplication
    {
        #region Main Method

        /// <summary>
        /// Application Entry Point.
        /// </summary>
        [STAThreadAttribute()]
        public static void Main()
        {
            RemoteTranslatorAssistentApp remoteTranslatorAssistentApp = new RemoteTranslatorAssistentApp();
            LocatorBootstrapper.ApplyComposer(new DefaultRuntimeWithPluginsComposer());
            ResourceDictionary resources = new ResourceDictionary();
            resources.Source = new Uri(@"pack://application:,,,/RemoteTranslator.AssistentApplication;component/ResourcesDictionary.xaml");
            remoteTranslatorAssistentApp.Resources.MergedDictionaries.Add(resources);
            remoteTranslatorAssistentApp.Run();
        }

        #endregion
    }


    public class DefaultRuntimeWithPluginsComposer : IComposer
    {
        #region IComposer Members

        public IEnumerable<ExportProvider> GetCustomExportProviders()
        {
            return null;
        }

        public ComposablePartCatalog InitializeContainer()
        {
            var catalog = new AggregateCatalog();
            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            var pluginsPath = PluginManager.PluginsDirectory;
            catalog.Catalogs.Add(new DirectoryCatalog(baseDirectory));
            catalog.Catalogs.Add(new DirectoryCatalog(baseDirectory, "*.exe"));
            // Add any DLL's in the addins folder (if it exists)
            if (Directory.Exists(pluginsPath))
            {
                // Within the main plugin folder, look for specific plugin folders
                foreach (string pluginDir in Directory.GetDirectories(pluginsPath, "*.util"))
                {
                    catalog.Catalogs.Add(new DirectoryCatalog(pluginDir, "*.dll"));
                }
            }
            return catalog;
        }

        #endregion
    }
}
